<?php

use yii\db\Migration;

class m000000_000018_create_article extends Migration
{
    public function up()
    {
        $this->createTable('{{%article}}', [
            'id'=>$this->primaryKey(), // PK
            'uid'=>$this->integer(), // 用户编号
            'cid'=>$this->integer(), // 分类
            'title'=>$this->string(256), // 标题
            'cover_img_fid'=>$this->integer(), // 封面图片文件编号
            'intro'=>$this->string(512), // 简介
            'content'=>$this->text(), // 内容
            'tags'=>$this->string(512), // 标签
            'status'=>$this->integer(), // 状态
            'add_time'=>$this->integer(), // 添加时间
            'pub_time'=>$this->integer(), // 发布时间
            'remark'=>$this->text(), // 备注：如审核拒绝的原因等
        ]);
        $this->createIndex('fk_article_user1_idx', '{{%article}}', ['uid']);
        try {
            $this->addForeignKey('fk_article_user1', '{{%article}}', ['uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
        }
        $this->createIndex('fk_article_article_category1_idx', '{{%article}}', ['cid']);
        try {
            $this->addForeignKey('fk_article_article_category1', '{{%article}}', ['cid'], '{{%article_category}}', ['id']);
        } catch (Exception $e) {
        }
        $this->createIndex('fk_article_upload_file1_idx', '{{%article}}', ['cover_img_fid']);
        try {
            $this->addForeignKey('fk_article_upload_file1', '{{%article}}', ['cover_img_fid'], '{{%upload_file}}', ['id']);
        } catch (Exception $e) {
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['article_status', 1, '草稿'],
            ['article_status', 2, '待审核'],
            ['article_status', 3, '已发布'],
            ['article_status', 4, '已拒绝'],
            ['article_status', 0, '已删除'],
        ]);
    }

    public function down()
    {
        $this->dropTable('{{%article}}');
    }
}
